package com.huazh.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Description://TODO
 * @Authoe: hzh
 * @Time: 2020/7/2817:24
 */
@RestController
public class ConsumerController {

    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private RestTemplate restTemplate;

//    @Value("${spring.application.name}")
//    private String appName;

    @GetMapping("/consumer/{id}")
    public String echoAppName(@PathVariable("id") Long id){
        //Access through the combination of LoadBalanceClient and RestTemplate
        ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
        String path = String.format("http://%s:%s/provider/%s",serviceInstance.getHost(),serviceInstance.getPort(), id);
        System.out.println("request path:" +path);
        return restTemplate.getForObject(path,String.class);
    }
}
